<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
       size="small"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
    >
      <el-form-item label="优惠卷类型" prop="couponType">
        <el-select v-model="dataForm.couponType" placeholder="请选择">
          <el-option label="全场赠券" :value="0"></el-option>
          <el-option label="会员赠券" :value="1"></el-option>
          <el-option label="购物赠券" :value="2"></el-option>
          <el-option label="注册赠券" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <!-- prop="couponImg" -->
      <el-form-item label="优惠券图片">
        <single-upload v-model="dataForm.couponImg"></single-upload>
      </el-form-item>
      <el-form-item label="优惠卷名字" prop="couponName">
        <el-input
          v-model="dataForm.couponName"
          placeholder="优惠卷名字"
        ></el-input>
      </el-form-item>
      <el-form-item label="数量" prop="num">
        <el-input-number :min="0" v-model="dataForm.num"></el-input-number>
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input-number
          :min="0"
          v-model="dataForm.amount"
          :precision="2"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="每人限领张数" prop="perLimit">
        <el-input-number :min="0" v-model="dataForm.perLimit"></el-input-number>
      </el-form-item>
      <el-form-item label="使用门槛（最小积分）" prop="minPoint">
        <el-input-number :min="0" v-model="dataForm.minPoint"></el-input-number>
      </el-form-item>
      <el-form-item label="有效时间" prop="useTimeRange">
        <el-date-picker
          v-model="dataForm.useTimeRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="使用类型" prop="useType">
        <el-select v-model="dataForm.useType" placeholder="请选择">
          <el-option :value="0" label="全场通用"></el-option>
          <el-option :value="1" label="指定分类"></el-option>
          <el-option :value="2" label="指定商品"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="dataForm.note" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="发行数量" prop="publishCount">
        <el-input-number
          v-model="dataForm.publishCount"
          :min="0"
        ></el-input-number>
      </el-form-item>
      <!-- prop="enableStartTime" -->
      <el-form-item label="领取日期">
        <el-date-picker
          v-model="dataForm.timeRange"
          type="daterange"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="优惠码" prop="code">
        <el-input v-model="dataForm.code" placeholder="优惠码"></el-input>
      </el-form-item>
      <el-form-item label="领取所需等级" prop="memberLevel">
        <el-select v-model="dataForm.memberLevel" placeholder="请选择">
          <el-option :value="0" label="不限制"></el-option>
          <el-option
            v-for="item in memberLevels"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
import { addOrUpdate } from "@/api/coupon";

export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      memberLevels: [],
      dataForm: {
        id: 0,
        couponType: "",
        couponImg: "",
        couponName: "",
        num: "",
        amount: "",
        perLimit: "",
        minPoint: "",
        startTime: "",
        endTime: "",
        useType: "",
        note: "",
        publishCount: "",
        useCount: "",
        receiveCount: "",
        enableStartTime: "",
        enableEndTime: "",
        code: "",
        memberLevel: "",
        publish: 0,
        timeRange: [],
        useTimeRange: [],
      },
      dataRule: {
        couponType: [
          {
            required: true,
            message: "优惠卷类型不能为空",
            trigger: "blur",
          },
        ],
        // couponImg: [
        //   { required: true, message: "优惠券图片不能为空", trigger: "blur" },
        // ],
        couponName: [
          { required: true, message: "优惠卷名字不能为空", trigger: "blur" },
        ],
        num: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        amount: [{ required: true, message: "金额不能为空", trigger: "blur" }],
        perLimit: [
          { required: true, message: "每人限领张数不能为空", trigger: "blur" },
        ],
        minPoint: [
          { required: true, message: "使用门槛不能为空", trigger: "blur" },
        ],
        useType: [
          {
            required: true,
            message: "使用类型不能为空",
            trigger: "blur",
          },
        ],
        note: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        publishCount: [
          { required: true, message: "发行数量不能为空", trigger: "blur" },
        ],
        // enableStartTime: [
        //   {
        //     required: true,
        //     message: "可以领取的开始日期不能为空",
        //     trigger: "blur",
        //   },
        // ],
        // enableEndTime: [
        //   {
        //     required: true,
        //     message: "可以领取的结束日期不能为空",
        //     trigger: "blur",
        //   },
        // ],
        code: [{ required: true, message: "优惠码不能为空", trigger: "blur" }],
        memberLevel: [
          {
            required: true,
            message: "可以领取的会员等级不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    // this.getMemberLevels();
  },
  methods: {
    getMemberLevels() {
      //获取所有的会员等级
      this.$http({
        url: this.$http.adornUrl("/member/memberlevel/list"),
        method: "get",
        params: this.$http.adornParams({
          page: 1,
          limit: 500,
        }),
      }).then(({ data }) => {
        this.memberLevels = data.page.list;
      });
    },

    init(data) {
      this.dataForm.id = 0;

      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (data) {
          this.dataForm.id = data.id;
          this.dataForm.couponType = data.couponType;
          this.dataForm.couponImg = data.couponImg;
          this.dataForm.couponName = data.couponName;
          this.dataForm.num = data.num;
          this.dataForm.amount = data.amount;
          this.dataForm.perLimit = data.perLimit;
          this.dataForm.minPoint = data.minPoint;
          this.dataForm.startTime = data.startTime;
          this.dataForm.endTime = data.endTime;
          this.dataForm.useType = data.useType;
          this.dataForm.note = data.note;
          this.dataForm.publishCount = data.publishCount;
          this.dataForm.useCount = data.useCount;
          this.dataForm.receiveCount = data.receiveCount;
          this.dataForm.enableStartTime = data.enableStartTime;
          this.dataForm.enableEndTime = data.enableEndTime;
          this.dataForm.code = data.code;
          this.dataForm.memberLevel = data.memberLevel;
          this.dataForm.publish = data.publish;
          this.dataForm.useTimeRange = [
            this.dataForm.enableStartTime,
            this.dataForm.enableEndTime,
          ];
          this.dataForm.timeRange = [
            this.dataForm.startTime,
            this.dataForm.endTime,
          ];
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          addOrUpdate({
            id: this.dataForm.id || "",
            couponType: this.dataForm.couponType,
            couponImg: this.dataForm.couponImg,
            couponName: this.dataForm.couponName,
            num: this.dataForm.num,
            amount: this.dataForm.amount,
            perLimit: this.dataForm.perLimit,
            minPoint: this.dataForm.minPoint,
            startTime: this.dataForm.useTimeRange[0],
            endTime: this.dataForm.useTimeRange[1],
            useType: this.dataForm.useType,
            note: this.dataForm.note,
            publishCount: this.dataForm.publishCount,
            useCount: this.dataForm.useCount,
            receiveCount: this.dataForm.receiveCount,
            enableStartTime: this.dataForm.timeRange[0],
            enableEndTime: this.dataForm.timeRange[1],
            code: this.dataForm.code,
            memberLevel: this.dataForm.memberLevel,
            publish: this.dataForm.publish,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
